﻿<!DOCTYPE html>
<html lang="zh">
<head>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta name="renderer" content="webkit">
    <meta http-equiv="CurrData.Type" content="text/html; charset=utf-8" />
    <meta charset="utf-8" />
    <title>Index </title>
    <link href="/favicon.ico" rel="shortcut icon" type="image/x-icon" />
    <meta name="viewport" content="width=device-width" />

    <link href="/js/kd/styles/kendo.common.min.css" rel="stylesheet" />
    <link href="/js/kd/styles/kendo.blueopal.min.css" rel="stylesheet" />
    <link href="/js/fa/css/font-awesome.min.css" rel="stylesheet" />
    <link href="/js/site.css?t=20221031" rel="stylesheet" />

    <script src="/js/kd/jquery.min.js"></script>
    <script src="/js/kd/kendo.web.min.js"></script>
    <script src="/js/kd/kendo.aspnetmvc.min.js"></script>
    <script src="/js/kd/cultures/kendo.culture.zh-CN.min.js"></script>
    <script src="/js/kd/messages/kendo.messages.zh-CN.min.js"></script>
    <script src="/js/kd/jszip.min.js"></script>

    
    <script src="/js/kendo_stdefine.js"></script>
    <script src="/js/site50.js"></script>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.17.0/xlsx.full.min.js"></script>


</head>
<body>
    <style>
        body, html {
            width: 100%;
            height: 100%;
            color: rgb(101, 173, 235);
            font-size: 12px;
            background-color: rgba( 13,20,84,1);
        }

        textarea {
            height: 60px !important;
        }

        .tdLabel75 {
            padding: 5px;
        }

        @media screen and (max-width: 1400px) {
            body, html {
                font-size: 10px;
            }
        }

        .flexcol {
            flex-direction: column;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .flexrow {
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .bgfull {
            background-size: 100% 100%;
        }

        :root {
            --var_color_B: rgb(11,203,237)
        }

        .colorB {
            color: var(--var_color_B);
        }

        .bgB {
            background-color: var(--var_color_B);
        }

        .myBtn {
            cursor: pointer;
            pointer-events: all;
        }

            .myBtn:hover {
                animation: myBtnScale 1s ease-in-out infinite alternate;
            }


        @keyframes myBtnScale {
            from {
                scale: 1;
            }

            to {
                scale: 1.2;
            }
        }
    </style>
    <style>
        .k-window {
            background-image: url(/img/windowbg.png) !important;
            background-size: 100% 100%;
            padding: 5px;
        }

        .k-window-content {
            overflow-y: auto;
        }

        .k-window-titlebar {
            background: linear-gradient(135deg, rgba(30, 144, 255, 0.8), rgba(0, 0, 139, 0.8)) !important;
            margin-top: -30px !important;
        }

        .k-window-iframecontent {
            height: calc(100% - 5px);
        }

        .k-window-actions {
            top: 8px !important;
        }
    </style>

    <style>
        body {
        }

        .k-splitter {
            position: absolute;
            left: 5px;
            top: 5px;
            width: calc(100% - 12px);
            height: calc(100% - 12px);
        }

        .k-splitbar-horizontal {
            width: 0px !important;
        }

        .tdValue {
            flex: 1;
        }

        i {
            cursor: pointer;
        }
    </style>

    <div id="divSplitter" data-role="splitter"
         data-panes="[
                { collapsible: false, resizable: false,  size: '245px' },
                { collapsible: false, resizable: false }
             ]"
         style="">
        <div id="left-pane" style="overflow:hidden;overflow-y:auto;">
            <div id="toolbarCenter" style="height:40px;line-height:40px;margin-bottom:2px;border-bottom: 1px solid #23549C;background:#14346a;font-family:'Microsoft YaHei'" class="flexrow">
                <div style="flex:1;margin-left:14px;font-size:14px">
                    <ul data-role="buttongroup"
                        data-index="0"
                        data-bind="  events: { select: buttongroup_Select }">

                        <li>协议</li>
                        <li>模拟</li>
                        <li>日志</li>
                    </ul>
                </div>

                <div style="width:20px;color:#5999D4;">
                    <a id="btnAdd" style="cursor:pointer;" title="添加" onclick="Add()"><i class="fa fa-lg fa-plus"></i></a>
                </div>
                <div style="width:20px;color:#5999D4;">

                </div>
                <div style="width:20px;color:#5999D4;">
                    <a id="btnDelete" style="cursor:pointer;" title="删除" onclick="Delete()"><i class="fa fa-lg fa-trash"></i></a>
                </div>




            </div>
            <div id="listView" style="flex-wrap: wrap;overflow-x:hidden;overflow-y:auto ;height:calc( 100% - 45px)"></div>


        </div>

        <div id="center" style="overflow: hidden;overflow-y:auto; ">



        </div>


    </div>



    <script>
        function myAjax(options) {
            if (options.win == undefined) {
                options.win = myGetTop();
            }
            if (options.async == undefined) options.async = true;
            if (!options.timeout) options.timeout = 30000;
            if (options.contentType == undefined) options.contentType = "application/json";
            if (options.type == undefined) options.type = "POST";
            if (options.contentType == "application/json") {
                if (typeof (options.data) != "string") {
                    options.data = JSON.stringify(options.data);
                    
                }
            }
            if (options.Alert == undefined) options.Alert = true;
            var opt = {
                before: function () {
                    kendo.ui.progress($("body"), true);

                },
                after: function () {
                    kendo.ui.progress($("body"), false);
                },
                timeout: options.timeout,
                contentType: options.contentType,
                type: options.type,
                dataType: 'json',
                async: options.async,
                cache: false,
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    opt2.after();
                    if (options.Alert) {
                        if (textStatus == "timeout") {
                            myAlert("请求超时");
                        }
                        else {
                            myAlert('错误.:' + textStatus + " " + errorThrown);
                        }
                    }
                    else {
                        opt2.err({ C: "-1", M: "" + textStatus + " " + errorThrown });
                    }

                },
                err: function (r) {
                    myAlert('错误:' + r.Message);
                }
            }
            var opt2 = $.extend(true, {}, opt, options);
            opt2.success = function (r) {
                opt2.after();

                if (r.Code == "" || r.C == "") {
                    if (options.success) {
                        options.success(r);
                    }
                }
                else {
                    opt2.err(r);
                }
            }
            opt2.before();
            options.win.$.ajax(opt2);
        }
        myAjaxList = function (opt) {
            var oldsuccess = opt.success;
            opt.success = (r) => {
                for (var i = 0; i < r.Data.length; i++) {
                    r.Data[i].RowNo = i + 1;
                }
                oldsuccess(r);
            }
            myAjax(opt);
        }
    </script>
    <style>
        .lb_product2 {
            margin: 10px;
            line-height: 25px;
            border-bottom: 1px dashed #143c75;
            cursor: pointer;
        }

            .lb_product2:hover:not(.k-state-selected) {
                background: #18477f80 !important;
            }

        .k-listview {
            border: none;
        }

        .divtitle {
            height: 30px;
            background: #18477f80 !important;
            margin-top: 5px;
        }
    </style>
    <script type="text/x-kendo-template" id="listtemplate">
        <div class="lb_product2">
            <div class="flexrow">
                <div style="width:50px;text-align:center;">#:RowNo#、</div>
                <div style="flex:1;">#:Name#</div>
            </div>
            <div class="flexrow">
                <div style="flex:1;">#:ShortName#</div>
            </div>
        </div>
    </script>

    
    <script>
        var viewModel = new kendo.data.ObservableObject({
            buttongroup_Select: function (e) {
                var buttonGroup = e.sender;
                var index = buttonGroup.current().index();
                switch (index) {
                    case 0:
                        var url = "/index.html";
                        break;
                    case 1:
                        var url = "/indexsim.html";
                        break;
                    case 2:
                        var url = "/indexlog.html";
                        break;
                }
                window.location.href = url;
            }
        });
        var gridBaseOption = {
            autoBind: false,
            navigatable: true,
            pageable: false,
            editable: false,
            messages: $.extend(true, {}, KDGridmessages),
            sortable: true,
            selectable: "row",
            filterable: false,
        };

        
        var CurrData = {};
        var CurrDataSave = {};
        function listView_dataBound() {
            var listView = $("#listView").data("kendoListView");
            if (_currListViewId) {
                var item = listView.dataSource.get(_currListViewId);
                listView.select(".lb_product2[data-uid='" + item.uid + "'   ]");
            } else {
                listView.select(".lb_product2:first");
            }

        }
        function Init() {

            kendo.bind($("#divSplitter"), viewModel);

            $("#listView").kendoListView({
                autoBind: false,
                template: kendo.template($("#listtemplate").html()),
                selectable: "single",
                dataBound: function () {
                    listView_dataBound();
                },
                change: function () {
                    var selectedItem = this.dataItem(this.select());
                    if (selectedItem) {
                        showDetail(selectedItem);
                    }

                }
            });


            List("");
        }
        var _currListViewId = "";
        function List(selid) {
            $("#center").html("");
             
            _currListViewId = selid;
            var url = "/api/ProtocolDefine/list"
            myAjaxList({
                url: url,
                success: function (r) {

                    var dsoptions = {
                        schema: {
                            model: {
                                id: "Id",
                                fields: { Id: { editable: false, nullable: true } }
                            }
                        },
                        data: r.Data,
                    };

                    var gridDataSource = new kendo.data.DataSource(dsoptions);
                    var listView = $("#listView").data("kendoListView");
                    listView.setDataSource(gridDataSource);
                    gridDataSource.read();
                }
            })
        }


        function closeWindow(sender) {
            var win = $(sender).closest(".k-window-content");
            win.data("kendoWindow").close();
        }
        function updown_Click(sender) {
            var obj = $(sender);
            var p = obj.closest(".divtitle");

            if (obj.hasClass("fa-chevron-up")) {
                obj.removeClass("fa-chevron-up");
                obj.addClass("fa-chevron-down");
                var nextElement = p[0].nextElementSibling; // 获取p的同级下一个元素

                $(nextElement).toggle();
            } else {
                obj.removeClass("fa-chevron-down");
                obj.addClass("fa-chevron-up");
                var nextElement = p[0].nextElementSibling; // 获取p的同级下一个元素
                $(nextElement).toggle();
            }
        }
        function showDetail(m) {
          
            myAjax({
                url: "/api/ProtocolDefine/Detail",
                data: { Id: m.Id },
                success: function (r) {

                    CurrData = r.Data;
                    if (!CurrData.SJZD) {
                        CurrData.SJZD = []
                    }

                    if (!CurrData.GDSJ) {
                        CurrData.GDSJ = []
                    }
                    if (!CurrData.XHSJ) {
                        CurrData.XHSJ = []
                    }
                    showDetail2(m);
                }
            })
        }
        
        function showDetail2(m) {
             

            var s = `<div style="height:5px;"></div>
<div class="flexrow divtitle">
                                                <div style="width:20px;"></div>
                                                <div style="color: #40b4e4;width:20px;">
                                                    <i class="fa fa-circle"></i>
                                                </div>
                                                <div style="width:100px;font-weight:bold;">基本信息  </div>
                                                <div style="flex:1;">
                                                    <a id="btnEdit" style="cursor:pointer;" title="修改" onclick="Edit()"><i class="fa fa-lg fa-pencil"></i></a>
                                                </div>
                                                <div style="width:20px;">
                                                      <i class="fa fa-chevron-up" onclick="updown_Click(this)"></i>
                                                </div>
</div>
<div id="jbxx">
<div class="flexrow">
                                                <div class="tdLabel75">名称</div>
                                                <div class="tdValue">
                                                    <input class="k-textbox" readonly="readonly" value="${m.Name}" />
                                                </div>
                                                <div style="width:20px;"></div>
</div>

<div class="flexrow">
                                                <div class="tdLabel75">简称</div>
                                                <div class="tdValue">
                                                    <input class="k-textbox" readonly="readonly" value="${m.ShortName}" />
                                                </div>
                                                <div style="width:20px;"></div>
</div>

<div class="flexrow">
                                                <div class="tdLabel75">备注</div>
                                                <div class="tdValue">
                                                    <textarea class="k-textbox" readonly="readonly" >${m.Memo}</textarea>
                                                </div>
                                                <div style="width:20px;"></div>
</div>
</div>

 
`;
           

            s += `<div class="flexrow divtitle">
                                                <div style="width:20px;"></div>
                                                <div style="color: #40b4e4;width:20px;">
                                                    <i class="fa fa-circle"></i>
                                                </div>
                                                <div style="width:100px;font-weight:bold;">数据字典  </div>
                                                <div style="flex:1;">
                                                    <a id="btnAdd" style="cursor:pointer;" title="添加" onclick="AddSJZD()"><i class="fa fa-lg fa-plus"></i></a>
                                                </div>
                                                <div style="width:20px;">
                                                  <i class="fa fa-chevron-up" onclick="updown_Click(this)"></i>
                                                </div>
</div>`;

            s += `
<div>`
            for (var i = 0; i < CurrData.SJZD.length; i++) {
                var data = GetSJZDMemo(CurrData.SJZD[i]);
                s += `<div class="flexrow" style="margin-top:5px;">


                                                           <div  class="tdLabel75">${CurrData.SJZD[i].Name}
                                                           </div>
                                                           <div class="tdValue">
                                                                <textarea class="k-textbox" readonly  >${data}</textarea>
                                                            </div>
                                                            <div style="width:60px;">
                                                             <a id="btnAdd" style="cursor:pointer;margin-left:5px;" title="修改" onclick="EditSJZD('${CurrData.SJZD[i].Id}')"><i class="fa fa-lg fa-pencil"></i></a>
                                                             <a id="btnAdd" style="cursor:pointer;margin-left:5px;" title="删除" onclick="DeleteSJZD('${CurrData.SJZD[i].Id}')"><i class="fa fa-lg fa-remove"></i></a>
                                                            </div>

                                                    </div>`;


            }

            s += `
</div>`


         


 


            s += `<div class="flexrow divtitle">
                                                <div style="width:20px;"></div>
                                                <div style="color: #40b4e4;width:20px;">
                                                    <i class="fa fa-circle"></i>
                                                </div>
                                                <div style="width:100px;font-weight:bold;">固定格式数据</div>
                                                <div style="flex:1;">
                                                   <a id="btnAdd" style="cursor:pointer;" title="添加" onclick="AddGDSJ()"><i class="fa fa-lg fa-plus"></i></a>
                                                </div>
                                                <div style="width:20px;">
                                                   <i class="fa fa-chevron-up" onclick="updown_Click(this)"></i>
                                                </div>
</div>
<div>
`;
            for (var i = 0; i < CurrData.GDSJ.length; i++) {
                s += `<div class="flexrow">
                                                <div class="tdLabel75"> 
                                                ${CurrData.GDSJ[i].Name}
                                                </div>
                                                <div class="tdValue">
                                                    <input class="k-textbox" readonly="readonly" value="${GetGDSJMemo(CurrData.GDSJ[i])}" />
                                                </div>
                                                <div style="width:60px;">
                                                   <a id="btnAdd" style="cursor:pointer;" title="修改" onclick="EditGDSJ(${CurrData.GDSJ[i].Id})"><i class="fa fa-lg fa-pencil"></i></a>
                                                   <a id="btnAdd" style="cursor:pointer;margin-left:5px;" title="删除" onclick="DeleteGDSJ('${CurrData.GDSJ[i].Id}')"><i class="fa fa-lg fa-remove"></i></a>
                                                </div>
                          </div> `;
            }
            s += `
</div>`;



            s += `<div class="flexrow divtitle">
                                                <div style="width:20px;"></div>
                                                <div style="color: #40b4e4;width:20px;">
                                                    <i class="fa fa-circle"></i>
                                                </div>
                                                <div style="width:100px;font-weight:bold;">循环数据</div>
                                                <div style="flex:1;">
                                                   <a id="btnAdd" style="cursor:pointer;" title="添加" onclick="AddXHSJ()"><i class="fa fa-lg fa-plus"></i></a>
                                                </div>
                                                <div style="width:20px;">
                                                   <i class="fa fa-chevron-up" onclick="updown_Click(this)"></i>
                                                </div>
</div>
<div>
`;
            for (var i = 0; i < CurrData.XHSJ.length; i++) {
                s += `<div class="flexrow">
                                                <div class="tdLabel75"> 
                                                ${CurrData.XHSJ[i].Name}
                                                </div>
                                                <div class="tdValue">
                                                    <input class="k-textbox" readonly="readonly" value="${GetXHSJMemo(CurrData.XHSJ[i])}" />
                                                </div>
                                                <div style="width:60px;">
                                                   <a id="btnAdd" style="cursor:pointer;" title="修改" onclick="EditXHSJ(${CurrData.XHSJ[i].Id})"><i class="fa fa-lg fa-pencil"></i></a>
                                                   <a id="btnAdd" style="cursor:pointer;margin-left:5px;" title="删除" onclick="DeleteXHSJ('${CurrData.XHSJ[i].Id}')"><i class="fa fa-lg fa-remove"></i></a>
                                                </div>
                          </div> `;
            }
            s += `
</div>`;


            $("#center").html(s);

        }
        function GetSJZDMemo(m) {
            var s = "";
            for (var i = 0; i < m.Data.length; i++) {
                var item = m.Data[i]
                s += `${item.Name}:`;
                if (item.IMin != null && item.IMin != undefined
                    && item.IMax != null && item.IMax != undefined
                ) {
                    if (item.IMin == item.IMax) {
                        s += `${item.IMin};`;
                    } else {
                        s += `${item.IMin}~${item.IMax};`;
                    }
                } else if (item.IMin != null && item.IMin != undefined) {
                    s += `>=${item.IMin};`;
                } else {
                    s += `<=${item.IMax};`;
                }
            }
            return s;
        }

         

        function GetGDSJMemo(m) {
            var s = `功能码：${m.Gnm} 开始地址:${m.StartAddress} `
            if (m.BitData) {
                s += ` 比特数据 `;
            }

            s+=` 数据数量:${ m.ReqNum }  `;
             
            s += ` 数据：`;
            for (var i = 0; i < m.Data.length; i++) {

                var o = m.Data[i];
                if (o.PropName != ""
                ) {
                    s += m.Data[i].PropName + " ";
                }
            }
            return s;
        }

        function GetXHSJMemo(m) {             
            var s = `功能码：${m.Gnm} 开始地址:${m.StartAddress} `
            if (m.BitData) {
                s += ` 比特数据 `;
            }

            s += ` 每组数据数量:${m.ReqNum}  `;

            s += ` 数据：`;
            for (var i = 0; i < m.Data.length; i++) {

                var o = m.Data[i];
                if (o.PropName != ""
                ) {
                    s += m.Data[i].PropName + " ";
                }
            }
            return s;
        }

    </script>

    <!--基本信息-->
    <div id="divAdd" style="display:none;">
        <input id="IsAdd" type="hidden" data-bind="value:IsAdd" />
        <form id="form1">
            <div class="flexrow">
                <div class="tdLabel75">名称</div>
                <div class="tdValue">
                    <input class="k-textbox" required data-bind="value:m.Name"
                           placeholder="协议名称" />
                </div>
                <div style="width:20px;"></div>
            </div>

            <div class="flexrow">
                <div class="tdLabel75">简称</div>
                <div class="tdValue">
                    <input class="k-textbox" data-bind="value:m.ShortName"
                           placeholder="协议简称" />
                </div>
                <div style="width:20px;"></div>
            </div>

            <div class="flexrow">
                <div class="tdLabel75">备注</div>
                <div class="tdValue">
                    <textarea class="k-textbox" data-bind="value:m.Memo" placeholder="协议备注"></textarea>
                </div>
                <div style="width:20px;"></div>
            </div>
        </form>


        <div style="height:20px;">

        </div>

        <div class="flexrow">
            <div style="flex:1;"></div>
            <div style="width:100px;">
                <a href="javascript:JBXX_Save();" class="k-button" style="width:80px;" accesskey="S">

                    <span style="text-decoration:underline;">S</span>
                    确定
                </a>
            </div>

            <div style="width:100px;">
                <a href="javascript:;" onclick="closeWindow(this);" class="k-button" style="width:80px;" accesskey="C">

                    <span style="text-decoration:underline;">C</span>
                    取消
                </a>
            </div>

            <div style="flex:1;"></div>
        </div>
    </div>
    <script>
        var viewModelJbxx = new kendo.data.ObservableObject({
            mold: {
                Id: "0",
                Name: "",
                ShortName: "",
                Memo: "",
                
            },
            m: {
            }
        });
        var myWindow;
        function Add() {
            myWindow = $("#divAdd");

            var v = myWindow.find("#form1")
            if (!v.data("kendoValidator")) {
                v.kendoValidator(kendoValidatorOptions)
            }
            viewModelJbxx.m = Object.assign({}, viewModelJbxx.mold);

            myWindow.kendoWindow({
                width: "600px",
                title: "添加协议",
                visible: false,
                modal: true,
                actions: [
                    "Close"
                ]
            }).data("kendoWindow").center().open();

            var m = Object.assign({}, viewModelJbxx.mold.toJSON());

            if (!v.data("kendoValidator")) {
                v.kendoValidator(kendoValidatorOptions)

            }
            else {

            }

            kendo.bind(myWindow, viewModelJbxx);

        }
        function Edit() {
            myWindow = $("#divAdd");

            var listView = $("#listView").data("kendoListView");
            var di = listView.dataItem(listView.select());
            if (!di) {
                return;
            }



            viewModelJbxx.m = Object.assign({}, {
                Id: di.Id,
                Name: di.Name,
                ShortName: di.ShortName,
                Memo: di.Memo,
                
            });
            myWindow.kendoWindow({
                width: "600px",
                title: "修改协议",
                visible: false,
                modal: true,
                actions: [
                    "Close"
                ]
            }).data("kendoWindow").center().open();


            var v = myWindow.find("#form1")
            if (!v.data("kendoValidator")) {
                v.kendoValidator(kendoValidatorOptions)
            }
            else {

            }
            kendo.bind(myWindow, viewModelJbxx);
        }
        function Delete() {
            var listView = $("#listView").data("kendoListView");
            var di = listView.dataItem(listView.select());
            if (!di) {
                return;
            }

            myConfirm({
                message: `确定要删除协议 ${di.Name} 吗？`,
                done: function () {
                    myAjax({
                        url: "/api/ProtocolDefine/Delete",
                        data: di.toJSON(),
                        success: function (r) {
                            List();
                        }
                    })
                }
            });

        }

        function JBXX_Save() {

            var v = myWindow.find("#form1").data("kendoValidator");
            if (v.validate() == false) return;

            var data = viewModelJbxx.m;
            if (data.toJSON) {
                data = data.toJSON();
            }
            if (data.Id == 0) {
                myAjax({
                    url: "/api/ProtocolDefine/Save",
                    data: data,
                    success: function (r) {
                        myWindow.data("kendoWindow").close();
                        List(r.Data.Id)
                    }
                })
            }
            else {
                CurrDataSave = Object.assign(CurrData, data);
             
                SaveAll();
            }

        }
        function SaveAll() {
            var data = CurrDataSave;
            if (data.toJSON) {
                data = data.toJSON();
            }
            myAjax({
                url: "/api/ProtocolDefine/Save",
                data: data,
                before: () => {
                    if (myWindow) {
                        kendo.ui.progress(myWindow, true);
                    }
                },
                after: () => {
                    if (myWindow) {
                        kendo.ui.progress(myWindow, false);
                    }
                },
                success: function (r) {
                    if (myWindow && myWindow.data("kendoWindow")) {
                        myWindow.data("kendoWindow").close();
                    }
                    List(r.Data.Id);
                }
            })
        }

    </script>

     



    <!--数据字典-->
    <div id="divSJZD" style="display: none; max-height: 90.740160vh !important; overflow-y: auto; ">

        <div class="flexrow">
            <div class="tdLabel75">字典名称</div>
            <div class="tdValue">
                <input id="Name" style="width:150px;" />
            </div>
            <div style="width:20px;"></div>
        </div>

        <div id="grid" style="height: 34.351632vh !important; "></div>


        <div style="height:20px;">

        </div>

        <div class="flexrow">
            <div style="flex:1;"></div>
            <div style="width:100px;">
                <input type="file" id="fileInputSJZD" style="display:none;" accept=".xlsx" />
                <a id="btnImport" class="k-button"
                   style="cursor:pointer;margin-left:10px;" title="导入"
                   onclick="document.getElementById('fileInputSJZD').click()"><i class="fa fa-lg fa-upload"></i>导入</a>

            </div>

            <div style="width:100px;">
                <a id="btnExport" class="k-button"
                   style="cursor:pointer;margin-left:10px;" title="导出"
                   onclick="exportToExcelSJZD()"><i class="fa fa-lg fa-download"></i>导出</a>
            </div>


            <div style="width:100px;">
                <a href="javascript:SaveSJZD();" class="k-button k-primary" style="width:80px;" accesskey="S">

                    <span style="text-decoration:underline;">S</span>
                    确定
                </a>
            </div>

            <div style="width:100px;">
                <a href="javascript:;" onclick="closeWindow(this);" class="k-button" style="width:80px;" accesskey="C">

                    <span style="text-decoration:underline;">C</span>
                    取消
                </a>
            </div>

            <div style="flex:1;"></div>
        </div>
    </div>
    <script>
        var viewModelSJZD = new kendo.data.ObservableObject({
            NameSource: [
                { Name: "监测类型" },
                { Name: "监测单位" },
                { Name: "设备状态" },
                { Name: "开关量状态" },
            ],
            BJStatusSource: [
                { Name: "正常" },
                { Name: "故障" },
                { Name: "一级报警" },
                { Name: "二级报警" },
            ],

            mold: {
                Id:0,
                Name: "",
                LX: "状态",
                Data: []
            },
            IsAdd: false,
        });
        function BJStatusEditor(container, options) {
            $('<input required name="BJStatus">')
                .appendTo(container)
                .kendoDropDownList({
                    autoBind: false,
                    dataTextField: "Name",
                    dataValueField: "Name",
                    dataSource: {
                        data: viewModelSJZD.BJStatusSource
                    }
                });
        }
        function IMinEditor(container, options) {
            $('<input   name="IMin">')
                .appendTo(container)
                .kendoNumericTextBox({
                    max: 65535,
                    min: 0
                });
        }
        function IMaxEditor(container, options) {
            $('<input   name="IMax">')
                .appendTo(container)
                .kendoNumericTextBox({
                    max: 65535,
                    min: 0
                });
        }


        function AddSJZD() {
            myWindow = $("#divSJZD");
            viewModelSJZD.IsAdd = true;
            var listView = $("#listView").data("kendoListView");
            var di = listView.dataItem(listView.select());
            if (!di) {
                return;
            }
          
            viewModelSJZD.m = {
                Id:0,
                Name: "",
                LX: "状态",
                Data: []
            }

            myWindow.kendoWindow({
                width: "600px",
                title: "添加数据字典",
                visible: false,
                modal: true,
                actions: [
                    "Close"
                ]
            }).data("kendoWindow").center().open();


            var v = myWindow.find("#grid")
            if (!v.data("kendoGrid")) {
                myWindow.find("#Name").kendoComboBox({
                    autoBind: false,
                    dataTextField: "Name",
                    dataValueField: "Name",
                    dataSource: viewModelSJZD.NameSource,
                    change: function (e) {

                    }
                });
                var opt = Object.assign({}, gridBaseOption);
                opt.editable = true;
                opt.columns = [
                    { title: '名字', field: "Name", width: 50, sortable: false, filterable: false, },
                    {
                        title: '低值', field: "IMin", width: 50, sortable: false, filterable: false,
                        editor: IMinEditor
                    },
                    {
                        title: '高值', field: "IMax", width: 50, sortable: false, filterable: false,
                        editor: IMaxEditor
                    },
                    {
                        title: '报警状态', field: "BJStatus", width: 50, sortable: false, filterable: false,
                        editor: BJStatusEditor
                    }

                ]
                var grid = myWindow.find("#grid").kendoGrid(opt).data("kendoGrid");
            }


            myWindow.find("#Name").data("kendoComboBox").text(viewModelSJZD.m.Name);
            for (var i = 0; i < 10; i++) {
                viewModelSJZD.m.Data.push({
                    Name: "",
                    IMin: 0,
                    IMax: 0,
                    BJStatus: "",
                })
            }
            var grid = myWindow.find("#grid").data("kendoGrid");
            var dsoptions = {
                schema: {
                    model: {
                        id: "Id",
                        fields: { Id: { editable: false, nullable: true } }
                    }
                },
                data: viewModelSJZD.m.Data,
            };
            var gridDataSource = new kendo.data.DataSource(dsoptions);
            grid.setDataSource(gridDataSource);
            gridDataSource.read();
        }
        var currSJZD = null;
        function EditSJZD(Id) {

            myWindow = $("#divSJZD");
            viewModelSJZD.IsAdd = false;
            var listView = $("#listView").data("kendoListView");
            var di = listView.dataItem(listView.select());
            if (!di) {
                return;
            }
           
            for (var i = 0; i < CurrData.SJZD.length; i++) {
                if (CurrData.SJZD[i].Id == Id) {
                    currSJZD = CurrData.SJZD[i];
                    var m = Object.assign({}, CurrData.SJZD[i]);
                    viewModelSJZD.set("m", m);
                    break;
                }
            }


            myWindow.kendoWindow({
                width: "600px",
                title: "修改数据字典",
                visible: false,
                modal: true,
                actions: [
                    "Close"
                ]
            }).data("kendoWindow").center().open();


            var v = myWindow.find("#grid")
            if (!v.data("kendoGrid")) {
                myWindow.find("#Name").kendoComboBox({
                    autoBind: false,
                    dataTextField: "Name",
                    dataValueField: "Name",
                    dataSource: viewModelSJZD.NameSource,
                    change: function (e) {

                    }
                });

                var opt = Object.assign({}, gridBaseOption);
                opt.editable = true;
                opt.columns = [
                    { title: '名字', field: "Name", width: 50, sortable: false, filterable: false, },
                    {
                        title: '低值', field: "IMin", width: 50, sortable: false, filterable: false,
                        editor: IMinEditor
                    },
                    {
                        title: '高值', field: "IMax", width: 50, sortable: false, filterable: false,
                        editor: IMaxEditor
                    },
                    {
                        title: '报警状态', field: "BJStatus", width: 50, sortable: false, filterable: false,
                        editor: BJStatusEditor
                    }

                ]
                var grid = myWindow.find("#grid").kendoGrid(opt).data("kendoGrid");
            }
            else {

            }
            myWindow.find("#Name").data("kendoComboBox").text(viewModelSJZD.m.Name);



            for (var i = 0; i < 10; i++) {
                viewModelSJZD.m.Data.push({
                    Name: "",
                    IMin: 0,
                    IMax: 0,
                    BJStatus: "",
                })
            }
            var grid = myWindow.find("#grid").data("kendoGrid");
            var dsoptions = {
                schema: {
                    model: {
                        id: "Id",
                        fields: { Id: { editable: false, nullable: true } }
                    }
                },
                data: viewModelSJZD.m.Data,
            };
            var gridDataSource = new kendo.data.DataSource(dsoptions);
            grid.setDataSource(gridDataSource);
            gridDataSource.read();
        }

        function DeleteSJZD(Id) {
            myWindow = $("#divSJZD");

            var listView = $("#listView").data("kendoListView");
            var di = listView.dataItem(listView.select());
            if (!di) {
                return;
            }
            var data = {}
            for (var i = 0; i < CurrData.SJZD.length; i++) {
                if (CurrData.SJZD[i].Id == Id) {
                    data = CurrData.SJZD[i];
                    break;
                }
            }
            CurrDataSave = JSON.parse(JSON.stringify(CurrData))

            myConfirm({
                message: `确定要删除数据字典 ${data.Name} 吗？`,
                done: function () {
                    for (var i = 0; i < CurrDataSave.SJZD.length; i++) {
                        if (CurrDataSave.SJZD[i].Id == Id) {
                            CurrDataSave.SJZD.splice(i, 1);  // 删除匹配的元素
                            break;
                        }
                    }
                    for (var i = 0; i < CurrDataSave.SJZD.length; i++) {
                        CurrDataSave.SJZD[i].Id = i + 1;
                    }
                    SaveAll();
                }
            }); 
        }

        function SaveSJZD(){ 
            viewModelSJZD.m.Name = $.trim(myWindow.find("#Name").data("kendoComboBox").text());
            if (!viewModelSJZD.m.Name) {
                myAlert("字典名称不能为空");
                return;
            }
           
            var grid = myWindow.find("#grid").data("kendoGrid")
            var ds = grid.dataSource;
            var data = ds.data();
            var data2 = [];
            for (var i = 0; i < data.length; i++) {
                if (data[i].Name) {
                    if (data[i].IMin && data[i].IMax) {
                        if (data[i].IMin > data[i].IMax) {
                            myAlert("数据错误");
                            return;
                        }
                    }
                    data2.push(data[i]);
                }
            }

            data = data2;


            for (var i = 0; i < data.length; i++) {
                if (data[i].IMin != null && data[i].IMax != null) {
                    if (data[i].IMin > data[i].IMax) {
                        myAlert(`最小值最大值错误 ${data[i].IMin}:${data[i].IMax}`);
                    }
                }
            }
            // 检查区间是否重复
            for (let i = 0; i < data.length; i++) {
                for (let j = i + 1; j < data.length; j++) {
                    if (data[i].IMin < data[j].IMax && data[i].IMax > data[j].IMin) {
                        myAlert(`区间重复: [${data[i].IMin}, ${data[i].IMax}] 与 [${data[j].IMin}, ${data[j].IMax}]`);
                        return;
                    }
                }
            }


         
            viewModelSJZD.m.Data = data;
            var data = viewModelSJZD.m;
            if (data.toJSON) {
                data = data.toJSON();
            }
            CurrDataSave = JSON.parse(JSON.stringify(CurrData))

            if (viewModelSJZD.m.IsAdd) {
                for (var i = 0; i < CurrDataSave.SJZD.length; i++) {
                    if (CurrDataSave.SJZD[i].Name == data.Name) {
                        myAlert("名称重复");
                        return;
                    }
                }
            }
            else {
                for (var i = 0; i < CurrDataSave.SJZD.length; i++) {
                    if (CurrDataSave.SJZD[i].Name == data.Name
                        && CurrDataSave.SJZD[i].Id != data.Id
                    ) {
                        myAlert("名称重复");
                        return;
                    }
                }
            }
            if (viewModelSJZD.IsAdd) {
                data.Id = CurrDataSave.SJZD.length + 1;
                CurrDataSave.SJZD.push(data);
            } else {
                for (var i = 0; i < CurrDataSave.SJZD.length; i++) {
                    if (CurrDataSave.SJZD[i].Id == currSJZD.Id) {
                        CurrDataSave.SJZD[i] = data;
                        break;
                    }
                }
            }

            SaveAll();
        }


        // 添加文件读取功能
        document.getElementById('fileInputSJZD').addEventListener('change', function (e) {
            var file = e.target.files[0];
            if (!file) return;

            var reader = new FileReader();
            reader.onload = function (e) {
                try {
                    var data = new Uint8Array(e.target.result);
                    var workbook = XLSX.read(data, { type: 'array' });
                    var firstSheetName = workbook.SheetNames[0];
                    var worksheet = workbook.Sheets[firstSheetName];
                    var json = XLSX.utils.sheet_to_json(worksheet, {
                        header: 1,  // 使用数组格式
                        defval: "", // 空单元格默认值
                        raw: false, // 处理公式和格式
                        blankrows: false // 包含空行
                    });

                    // 处理读取的数据
                    if (json.length > 1) {
                        // 这里添加处理逻辑，将数据填充到表格中
                        var data = []
                        for (var i = 1; i < json.length; i++) {

                            var m = {
                                Name: json[i][0],
                                IMin: null,
                                IMax: null,
                                BJStatus: ""
                            };
                            if (json[i][1].toString() != "") {
                                m.IMin = parseInt(json[i][1]);
                            }
                            if (json[i][2].toString() != "") {
                                m.IMax = parseInt(json[i][2]);
                            }
 
                            if (json[i][3].toString() != "") {
                                var s = json[i][3].toString();
                                if (viewModelSJZD.BJStatusSource.some(item => item.Name === s)) {
                                    m.BJStatus = s;
                                }
                            }
                            data.push(m);
                            viewModelSJZD.m.Data = data;
                            var grid = myWindow.find("#grid").data("kendoGrid");
                            var dsoptions = {
                                schema: {
                                    model: {
                                        id: "Id",
                                        fields: { Id: { editable: false, nullable: true } }
                                    }
                                },
                                data: viewModelSJZD.m.Data,
                            };
                            var gridDataSource = new kendo.data.DataSource(dsoptions);
                            grid.setDataSource(gridDataSource);
                            gridDataSource.read();
                        }
                    }
                }
                catch (error) {
                    myAlert("格式错误：" + error)
                }

            };
            reader.readAsArrayBuffer(file);
        });

        function exportToExcelSJZD() {
            // 获取数据
            var data = viewModelSJZD.m.Data;

            // 创建工作表数据
            var wsData = [
                ["名称", "低值", "高值", "报警状态"] // 表头
            ];

            // 填充数据
            data.forEach(function (item) {
                wsData.push([
                    item.Name || "",
                    item.IMin || "",
                    item.IMax || "",
                    item.BJStatus || ""
                ]);
            });

            // 创建工作表
            var ws = XLSX.utils.aoa_to_sheet(wsData);

            // 创建工作簿
            var wb = XLSX.utils.book_new();
            XLSX.utils.book_append_sheet(wb, ws, "Sheet1");

            // 导出文件
            XLSX.writeFile(wb, "数据字典.xlsx");
        }
    </script>





     

     

    <!--主设备属性数据-->
    <div id="divGDSJ" style="display: none; max-height: 90.740160vh !important; overflow-y: auto; ">

        <div class="flexrow">
            <div class="tdLabel75">数据名称</div>
            <div class="tdValue">
                <input id="Name" style="width:150px;"
                       class="k-textbox"
                       data-bind="value:m.Name" />
            </div>
            <div style="width:20px;"></div>
        </div>

        <div class="flexrow">
            <div class="tdLabel75">功能码</div>
            <div class="tdValue">
                <input id="Gnm" style="width:150px;"
                       data-role="numerictextbox"
                       data-min="0"
                       data-max="255"
                       data-format="N0"
                       data-bind="value:m.Gnm" />
            </div>
            <div style="width:20px;"></div>
        </div>
        <div class="flexrow">
            <div class="tdLabel75">开始地址</div>
            <div class="tdValue">

                <input id="StartAddress" style="width:150px;"
                       data-role="numerictextbox"
                       data-min="0"
                       data-max="65555"
                       data-format="N0"
                       data-bind="value:m.StartAddress" />
            </div>
            <div style="width:20px;"></div>
        </div>


        <div class="flexrow">
            <div class="tdLabel75"></div>
            <div class="tdValue">
                <input type="checkbox" name="GDSJ_BitData" id="GDSJ_BitData"
                       class="k-checkbox"
                       data-bind="checked:m.BitData,events:{change:CreateGDSJData}" />

                <label class="k-checkbox-label" for="GDSJ_BitData">是否Bit</label>
            </div>
            <div style="width:20px;"></div>
        </div>




        <div class="flexrow">
            <div class="tdLabel75">数据数量</div>
            <div class="tdValue">
                <input id="ReqNum" style="width:150px;"
                       data-role="numerictextbox"
                       data-min="0"
                       data-max="65555"
                       data-format="N0"
                       data-bind="value:m.ReqNum,events:{change:CreateGDSJData}" />
            </div>
            <div style="width:20px;"></div>
        </div>



        <div id="grid" style="height:50vh !important; "></div>


        <div style="height:20px;">

        </div>

        <div class="flexrow">
            <div style="flex:1;"></div>


            <div style="width:100px;">
                <a href="javascript:SaveGDSJ();" class="k-button k-primary" style="width:80px;" accesskey="S">

                    <span style="text-decoration:underline;">S</span>
                    确定
                </a>
            </div>

            <div style="width:100px;">
                <a href="javascript:;" onclick="closeWindow(this);" class="k-button" style="width:80px;" accesskey="C">

                    <span style="text-decoration:underline;">C</span>
                    取消
                </a>
            </div>

            <div style="flex:1;"></div>
        </div>
    </div>

 
    <script>
       
        var viewModelGDSJ = new kendo.data.ObservableObject({

            CreateGDSJData:function () {
                var BitData = viewModelGDSJ.m.BitData;
               
                var ReqNum = viewModelGDSJ.m.ReqNum;
                var data = [];
                for (var i = 1; i <= ReqNum; i++) {
                    
                        data.push({
                            Id: i,
                            
                            PropName: "数据" + i.toString(),
                            DataType: BitData ? "Bit" : "UInt16",
                            DataLen:1,
                            PropType: "值",                            
                            SJZDKey: ""
                        }); 
                }
                for (var j = 0; j < viewModelGDSJ.m.Data.length && j<data.length; j++) {
                    data[j].PropName = viewModelGDSJ.m.Data[j].PropName;                     
                    data[j].DataType = viewModelGDSJ.m.Data[j].DataType;
                    data[j].DataLen = viewModelGDSJ.m.Data[j].DataLen;
                    data[j].PropType = viewModelGDSJ.m.Data[j].PropType;
                    data[j].SJZDKey = viewModelGDSJ.m.Data[j].SJZDKey;
                  
                }

                    var grid = myWindow.find("#grid").data("kendoGrid");

                    var dsoptions = {
                        schema: {
                            model: {
                                id: "Id",
                                fields: { Id: { editable: false, nullable: true } }
                            }
                        },
                        data: data,
                    };
                    var gridDataSource = new kendo.data.DataSource(dsoptions);
                    grid.setDataSource(gridDataSource);
                    gridDataSource.read();
 
             },

            oldm: {
                Id: 0,
                Name:"固定数据1",
                Gnm: 3,
                StartAddress: 0,
                BitData:false,
                ReqNum: 10,
               
                Data: []
            },
            SJZD: [],
            CurrId: 0,
           
            PropType: [
                { Name: "状态" },
                { Name: "值" },
                { Name: "类型" },
                { Name: "单位" },
                { Name: "精度" },
                { Name: "上限" },
                { Name: "下限" },
            ],
            DataType: [
                { Name: "Bit", Len: 1 },
                { Name: "Byte", Len: 1 },
                { Name: "UInt16", Len: 2 },
                { Name: "Int", Len: 4 },
                { Name: "Float", Len: 4 }
            ],
            ZSBSX:[],
            m: {

            },
            IsAdd:false,
        });

     
        function DataTypeEditor(container, options) {
            $('<input required name="DataType">')
                .appendTo(container)
                .kendoDropDownList({
                    autoBind: false,
                    dataTextField: "Name",
                    dataValueField: "Name",
                    dataSource: {
                        data: viewModelGDSJ.DataType
                    }
                });
        }
        function PropTypeEditor(container, options) {
            $('<input required name="PropType">')
                .appendTo(container)
                .kendoDropDownList({
                    autoBind: false,
                    dataTextField: "Name",
                    dataValueField: "Name",
                    dataSource: {
                        data: viewModelGDSJ.PropType
                    }
                });
        }


        function SJZDKeyEditor(container, options) {
            $('<input   name="SJZDKey">')
                .appendTo(container)
                .kendoDropDownList({
                    autoBind: false,
                    dataTextField: "Name",
                    dataValueField: "Name",
                    dataSource: {
                        data: viewModelGDSJ.SJZD
                    }
                });
        }
        var currGDSJ;
        function AddGDSJ() {
            myWindow = $("#divGDSJ");
            viewModelGDSJ.IsAdd = true;
            var listView = $("#listView").data("kendoListView");
            var di = listView.dataItem(listView.select());
            if (!di) {
                return;
            }

            viewModelGDSJ.SJZD = [{ Name: "" }];
            for (var i = 0; i < CurrData.SJZD.length; i++) {
                viewModelGDSJ.SJZD.push({ Name: CurrData.SJZD[i].Name });
            }
            viewModelGDSJ.m = Object.assign({}, viewModelGDSJ.oldm);


            
            

            myWindow.kendoWindow({
                width: "1200px",
                title: "添加固定数据",
                visible: false,
                modal: true,
                actions: [
                    "Close"
                ]
            }).data("kendoWindow").center().open();


            var v = myWindow.find("#grid")
            if (!v.data("kendoGrid")) {
                var opt = Object.assign({}, gridBaseOption);
                opt.editable = true;
                opt.columns = [
                    
                    {
                        title: '编号', field: "Id", width: 50, sortable: false, filterable: false,
                        editable: function (dataItem) { return false }
                    },

                    {
                        title: '数据名称', field: "PropName", width: 100, sortable: false, filterable: false,
                        editable: () => { return true; }
                         
                    },
                    {
                        title: '数据格式', field: "DataType", width: 100, sortable: false, filterable: false,
                        editable: (row) => {                             
                            return row.DataType!="Bit";
                        },
                        editor: DataTypeEditor,

                    },


                    {
                        title: '对应类型', field: "PropType", width: 100, sortable: false, filterable: false,
                       
                        editor:PropTypeEditor
                    }
                    ,
                    {
                        title: '数据字典', field: "SJZDKey", width: 100, sortable: false, filterable: false,
                        editable: function (dataItem) { return dataItem.PropType == "状态" || dataItem.PropType == "类型" || dataItem.PropType == "单位" },
                        editor: SJZDKeyEditor
                      /*  template: $("#QQZSBSXFml-template").html()*/
                    }
                ]
                var grid = myWindow.find("#grid").kendoGrid(opt).data("kendoGrid");


                
            }
            else {

            }


           
            

            kendo.bind($("#divGDSJ"), viewModelGDSJ);
            viewModelGDSJ.CreateGDSJData();
        }
        function EditGDSJ(Id) {

            myWindow = $("#divGDSJ");
            viewModelGDSJ.IsAdd = false;
            viewModelGDSJ.SJZD = [{ Name: "" }];
            for (var i = 0; i < CurrData.SJZD.length; i++) {
                viewModelGDSJ.SJZD.push({ Name: CurrData.SJZD[i].Name });
            }

            for (var i = 0; i < CurrData.GDSJ.length; i++) {
                if (Id == CurrData.GDSJ[i].Id) {
                    viewModelGDSJ.m = Object.assign({}, CurrData.GDSJ[i]);
                    break;
                }
            }
            
             


            myWindow.kendoWindow({
                width: "1200px",
                title: "修改固定数据",
                visible: false,
                modal: true,
                actions: [
                    "Close"
                ]
            }).data("kendoWindow").center().open();


            var v = myWindow.find("#grid")
            if (!v.data("kendoGrid")) {
                var opt = Object.assign({}, gridBaseOption);
                opt.editable = true;
                opt.columns = [

                    {
                        title: '编号', field: "Id", width: 50, sortable: false, filterable: false,
                        editable: function (dataItem) { return false }
                    },

                    {
                        title: '数据名称', field: "PropName", width: 100, sortable: false, filterable: false,
                        editable: () => { return true; }

                    },
                    {
                        title: '数据格式', field: "DataType", width: 100, sortable: false, filterable: false,
                        editor: DataTypeEditor,

                    },


                    {
                        title: '对应类型', field: "PropType", width: 100, sortable: false, filterable: false,

                        editor: PropTypeEditor
                    }
                    ,
                    {
                        title: '数据字典', field: "SJZDKey", width: 100, sortable: false, filterable: false,
                        editable: function (dataItem) { return dataItem.PropType == "状态" || dataItem.PropType == "类型" || dataItem.PropType == "单位" },
                        editor: SJZDKeyEditor
                        /*  template: $("#QQZSBSXFml-template").html()*/
                    }
                ]
                var grid = myWindow.find("#grid").kendoGrid(opt).data("kendoGrid");



            }
            else {

            }








            kendo.bind($("#divGDSJ"), viewModelGDSJ);
            viewModelGDSJ.CreateGDSJData();

        }
        function DeleteGDSJ(Id) {

            var listView = $("#listView").data("kendoListView");
            var di = listView.dataItem(listView.select());
            if (!di) {
                return;
            }
            CurrDataSave = JSON.parse(JSON.stringify(CurrData))

            myConfirm({
                message: `确定要删除这个固定格式数据 吗？`,
                done: function () {
                    for (var i = 0; i < CurrDataSave.GDSJ.length; i++) {
                       
                        if (CurrDataSave.GDSJ[i].Id == Id) {
                            CurrDataSave.GDSJ.splice(i, 1);  // 删除匹配的元素
                            break;
                        }
                    }
                    for (var i = 0; i < CurrDataSave.GDSJ.length; i++) {
                        CurrDataSave.GDSJ[i].Id = i + 1;
                    }
                    SaveAll();
                }
            });





        }

         
        function SaveGDSJ() {
            var m = viewModelGDSJ.m;

            if (m.Gnm == null) {
                myAlert("功能码必须输入");
            }
            if (m.StartAddress == null) {
                myAlert("开始地址必须输入");
            }
            if (m.ReqNum == null) {
                myAlert("数据数量必须输入");
            }
          


            var grid = myWindow.find("#grid").data("kendoGrid")
            var ds = grid.dataSource;
            var data = ds.data();

            for (var i = 0; i < data.length; i++) {
 
                var o = data[i];
                if (!o.PropName) {
                    myAlert(`数据名称必须输入`);
                }
                if (!o.DataType) {
                    myAlert(`数据名称必须输入`);
                }
                for (var j = 0; j < viewModelGDSJ.DataType.length; j++) {
                    if (o.DataType == viewModelGDSJ.DataType[j].Id) {
                        o.DataLen = viewModelGDSJ.DataType[j].Len;
                        break;
                    }
                }
                if (!o.PropType) {
                    myAlert(`${o.Name} 必须选择类型`);
                    return;
                }

                 
                
                if (o.PropType == "状态" || o.PropType == "类型" || o.PropType == "单位") {
                    if (!o.SJZDKey) {
                        myAlert(`${o.PropName} 必须选择数据字典`);
                        return;
                    }
                }
                 
            }


            if (data.toJSON) {
                data = data.toJSON();
            }
             
            m.Data = data;
           
            if (m.toJSON) {
                m = m.toJSON();
            }
            CurrDataSave = JSON.parse( JSON.stringify(CurrData) )
           
            if (viewModelGDSJ.IsAdd) {
                for (var i = 0; i < CurrDataSave.GDSJ.length; i++) {
                    CurrDataSave.GDSJ[i].Id = i + 1;
                }
                m.Id = CurrDataSave.GDSJ.length + 1;
                CurrDataSave.GDSJ.push(m);
            } else {
                for (var i = 0; i < CurrDataSave.GDSJ.length; i++) {
                    if (CurrDataSave.GDSJ[i].Id ==  m.Id) {
                        CurrDataSave.GDSJ[i] = m;
                    }
                    CurrDataSave.GDSJ[i].Id = i + 1;
                }
            }

            SaveAll();
             
        }
       

    </script>


    <!--请求子设备属性-->

    <div id="divXHSJ" style="display: none; max-height: 90.740160vh !important; overflow-y: auto;line-height:25px; ">

        <div class="flexrow">
            <div class="tdLabel75">数据名称</div>
            <div class="tdValue">
                <input id="Name" style="width:150px;"
                       class="k-textbox"
                       data-bind="value:m.Name" />
            </div>
            <div style="width:20px;"></div>
        </div>

        <div class="flexrow">
            <div class="tdLabel75">功能码</div>
            <div class="tdValue">
                <input id="Gnm" style="width:150px;"
                       data-role="numerictextbox"
                       data-min="0"
                       data-max="255"
                       data-format="N0"
                       data-bind="value:m.Gnm" />
            </div>
            <div style="width:20px;"></div>
        </div>
        <div class="flexrow">
            <div class="tdLabel75">开始地址</div>
            <div class="tdValue">

                <input id="StartAddress" style="width:150px;"
                       data-role="numerictextbox"
                       data-min="0"
                       data-max="65555"
                       data-format="N0"
                       data-bind="value:m.StartAddress" />
            </div>
            <div style="width:20px;"></div>
        </div>


        <div class="flexrow">
            <div class="tdLabel75"></div>
            <div class="tdValue">
                <input type="checkbox" name="QQZSBSX_BitData" id="QQZSBSX_BitData"
                       class="k-checkbox"
                       data-bind="checked:m.BitData,events:{change:CreateXHSJData}" />

                <label class="k-checkbox-label" for="QQZSBSX_BitData">是否Bit</label>
            </div>
            <div style="width:20px;"></div>
        </div>




        <div class="flexrow">
            <div class="tdLabel75">每组数据数量</div>
            <div class="tdValue">
                <input id="ReqNum" style="width:150px;"
                       data-role="numerictextbox"
                       data-min="0"
                       data-max="65555"
                       data-format="N0"
                       data-bind="value:m.ReqNum,events:{change:CreateXHSJData}" />
            </div>
            <div style="width:20px;"></div>
        </div>


       


        <div id="grid" style="height:50vh !important; "></div>

        <div style="height:20px;">

        </div>

        <div class="flexrow">
            <div style="flex:1;"></div>


            <div style="width:100px;">
                <a href="javascript:SaveXHSJ();" class="k-button k-primary" style="width:80px;" accesskey="S">

                    <span style="text-decoration:underline;">S</span>
                    确定
                </a>
            </div>

            <div style="width:100px;">
                <a href="javascript:;" onclick="closeWindow(this);" class="k-button" style="width:80px;" accesskey="C">

                    <span style="text-decoration:underline;">C</span>
                    取消
                </a>
            </div>

            <div style="flex:1;"></div>
        </div>
    </div>
    <script>
        var viewModelXHSJ = new kendo.data.ObservableObject({
            CreateXHSJData: function () {
                var BitData = viewModelXHSJ.m.BitData;

                var ReqNum = viewModelXHSJ.m.ReqNum;
                var data = [];
                for (var i = 1; i <= ReqNum; i++) {

                    data.push({
                        Id: i,

                        PropName: "数据" + i.toString(),
                        DataType: BitData ? "Bit" : "UInt16",
                        DataLen: 1,
                        PropType: "值",
                        SJZDKey: ""
                    });
                }
                for (var j = 0; j < viewModelXHSJ.m.Data.length && j < data.length; j++) {
                    data[j].PropName = viewModelXHSJ.m.Data[j].PropName;
                    data[j].DataType = viewModelXHSJ.m.Data[j].DataType;
                    data[j].DataLen = viewModelXHSJ.m.Data[j].DataLen;
                    data[j].PropType = viewModelXHSJ.m.Data[j].PropType;
                    data[j].SJZDKey = viewModelXHSJ.m.Data[j].SJZDKey;

                }

                var grid = myWindow.find("#grid").data("kendoGrid");

                var dsoptions = {
                    schema: {
                        model: {
                            id: "Id",
                            fields: { Id: { editable: false, nullable: true } }
                        }
                    },
                    data: data,
                };
                var gridDataSource = new kendo.data.DataSource(dsoptions);
                grid.setDataSource(gridDataSource);
                gridDataSource.read();

            },

            oldm: {
                Id: 0,
                Name: "循环数据1",
                Gnm: 3,
                StartAddress: 0,
                BitData: false,
                ReqNum: 10,

                Data: []
            },
            m: {}, 
            CurrId: 0,
            SJZD: [],
            IsAdd: false,
        });
        
        function AddXHSJ() {
            myWindow = $("#divXHSJ");
            viewModelXHSJ.IsAdd = true;

            var listView = $("#listView").data("kendoListView");
            var di = listView.dataItem(listView.select());
            if (!di) {
                return;
            }

            viewModelGDSJ.SJZD = [{ Name: "" }];
            for (var i = 0; i < CurrData.SJZD.length; i++) {
                viewModelGDSJ.SJZD.push({ Name: CurrData.SJZD[i].Name });
            }
            viewModelXHSJ.m = Object.assign({}, viewModelXHSJ.oldm);
            viewModelXHSJ.DataType = viewModelGDSJ.DataType;
            viewModelXHSJ.PropType = viewModelGDSJ.PropType;

            


            myWindow.kendoWindow({
                width: "1200px",
                title: "添加循环数据",
                visible: false,
                modal: true,
                actions: [
                    "Close"
                ]
            }).data("kendoWindow").center().open();


            var v = myWindow.find("#grid")
            if (!v.data("kendoGrid")) {
                var opt = Object.assign({}, gridBaseOption);
                opt.editable = true;
                opt.columns = [

                    {
                        title: '编号', field: "Id", width: 50, sortable: false, filterable: false,
                        editable: function (dataItem) { return false }
                    },

                    {
                        title: '数据名称', field: "PropName", width: 100, sortable: false, filterable: false,
                        editable: () => { return true; }

                    },
                    {
                        title: '数据格式', field: "DataType", width: 100, sortable: false, filterable: false,
                        editor: DataTypeEditor,

                    },


                    {
                        title: '对应类型', field: "PropType", width: 100, sortable: false, filterable: false,

                        editor: PropTypeEditor
                    }
                    ,
                    {
                        title: '数据字典', field: "SJZDKey", width: 100, sortable: false, filterable: false,
                        editable: function (dataItem) { return dataItem.PropType == "状态" || dataItem.PropType == "类型" || dataItem.PropType == "单位" },
                        editor: SJZDKeyEditor
                        /*  template: $("#QQZSBSXFml-template").html()*/
                    }
                ]
                var grid = myWindow.find("#grid").kendoGrid(opt).data("kendoGrid");



            }
            else {

            }


            kendo.bind($("#divXHSJ"), viewModelXHSJ);
            viewModelXHSJ.CreateXHSJData();
        }
        function EditXHSJ(Id) {             
            myWindow = $("#divXHSJ");
            viewModelXHSJ.IsAdd = false;
            var listView = $("#listView").data("kendoListView");
            var di = listView.dataItem(listView.select());
            if (!di) {
                return;
            }

            viewModelGDSJ.SJZD = [{ Name: "" }];
            for (var i = 0; i < CurrData.SJZD.length; i++) {
                viewModelGDSJ.SJZD.push({ Name: CurrData.SJZD[i].Name });
            }

            for (var i = 0; i < CurrData.XHSJ.length; i++) {
                if (CurrData.XHSJ[i].Id == Id) {
                 
                    viewModelXHSJ.m = Object.assign({}, CurrData.XHSJ[i]);
                   
                }
            }
            viewModelXHSJ.DataType = viewModelGDSJ.DataType;
            viewModelXHSJ.PropType = viewModelGDSJ.PropType;
         
            //Id  Name  PropId   PropName  


            myWindow.kendoWindow({
                width: "1200px",
                title: "修改循环数据",
                visible: false,
                modal: true,
                actions: [
                    "Close"
                ]
            }).data("kendoWindow").center().open();


            var v = myWindow.find("#grid")
            if (!v.data("kendoGrid")) {
                var opt = Object.assign({}, gridBaseOption);
                opt.editable = true;
                opt.columns = [

                    {
                        title: '编号', field: "Id", width: 50, sortable: false, filterable: false,
                        editable: function (dataItem) { return false }
                    },

                    {
                        title: '数据名称', field: "PropName", width: 100, sortable: false, filterable: false,
                        editable: () => { return true; }

                    },
                    {
                        title: '数据格式', field: "DataType", width: 100, sortable: false, filterable: false,
                        editor: DataTypeEditor,

                    },


                    {
                        title: '对应类型', field: "PropType", width: 100, sortable: false, filterable: false,

                        editor: PropTypeEditor
                    }
                    ,
                    {
                        title: '数据字典', field: "SJZDKey", width: 100, sortable: false, filterable: false,
                        editable: function (dataItem) { return dataItem.PropType == "状态" || dataItem.PropType == "类型" || dataItem.PropType == "单位" },
                        editor: SJZDKeyEditor
                        /*  template: $("#QQZSBSXFml-template").html()*/
                    }
                ]
                var grid = myWindow.find("#grid").kendoGrid(opt).data("kendoGrid");



            }
            else {

            }


            kendo.bind($("#divXHSJ"), viewModelXHSJ);
            viewModelXHSJ.CreateXHSJData();




        }
        function DeleteXHSJ(Id) {

            var listView = $("#listView").data("kendoListView");
            var di = listView.dataItem(listView.select());
            if (!di) {
                return;
            }

            CurrDataSave = JSON.parse(JSON.stringify(CurrData))
            myConfirm({
                message: `确定要删除这个循环数据 吗？`,
                done: function () {
                    for (var i = 0; i < CurrDataSave.XHSJ.length; i++) {

                        if (CurrDataSave.XHSJ[i].Id == Id) {
                            CurrDataSave.XHSJ.splice(i, 1);  // 删除匹配的元素
                            break;
                        }
                    }
                    for (var i = 0; i < CurrDataSave.XHSJ.length; i++) {
                        CurrDataSave.XHSJ[i].Id = i + 1;
                    }
                     
                    SaveAll();
                }
            });





        }
         
        function SaveXHSJ() {
            var m = viewModelXHSJ.m;

            if (m.Gnm == null) {
                myAlert("功能码必须输入");
            }
            if (m.StartAddress == null) {
                myAlert("开始地址必须输入");
            }
            if (m.ReqNum == null) {
                myAlert("数据数量必须输入");
            }



            var grid = myWindow.find("#grid").data("kendoGrid")
            var ds = grid.dataSource;
            var data = ds.data();

            for (var i = 0; i < data.length; i++) {

                var o = data[i];
                if (!o.PropName) {
                    myAlert(`数据名称必须输入`);
                }
                if (!o.DataType) {
                    myAlert(`数据名称必须输入`);
                }
                for (var j = 0; j < viewModelXHSJ.DataType.length; j++) {
                    if (o.DataType == viewModelXHSJ.DataType[j].Id) {
                        o.DataLen = viewModelXHSJ.DataType[j].Len;
                        break;
                    }
                }
                if (!o.PropType) {
                    myAlert(`${o.Name} 必须选择类型`);
                    return;
                }



                if (o.PropType == "状态" || o.PropType == "类型" || o.PropType == "单位") {
                    if (!o.SJZDKey) {
                        myAlert(`${o.PropName} 必须选择数据字典`);
                        return;
                    }
                }

            }


            if (data.toJSON) {
                data = data.toJSON();
            }

            m.Data = data;


            CurrDataSave = JSON.parse(JSON.stringify(CurrData))

            if (m.toJSON) {
                m = m.toJSON();
            }
            if (viewModelXHSJ.IsAdd) {
                for (var i = 0; i < CurrDataSave.XHSJ.length; i++) {
                    CurrDataSave.XHSJ[i].Id = i + 1;
                }
                m.Id = CurrDataSave.XHSJ.length + 1;
                CurrDataSave.XHSJ.push(m);
            } else {
                for (var i = 0; i < CurrDataSave.XHSJ.length; i++) {
                    if (CurrDataSave.XHSJ[i].Id ==  m.Id) {
                        CurrDataSave.XHSJ[i] = m;
                    }
                    CurrDataSave.XHSJ[i].Id = i + 1;
                }
            }

        
            SaveAll();
        }
       

    </script>


    <script>
        var SizeInfo;
        $(function () {

            kendo.culture("zh-CN");


            if (window.Init) {
                Init();
            }
        });
        function After50(d) {
            if (d) {
                setTimeout(function () { d(); }, 50);
            }
        }
    </script>
</body>
</html>
